package com.sun.timer;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.ApplicationContext;

import com.sun.cache.NoticeInfoCache;
import com.sun.dao.NoticeInfoMapper;
import com.sun.po.NoticeInfo;
import com.sun.push.BaiduPushMessage;
import com.sun.util.ApplicationContextFactory;

public class TimeTick {
	
	private static final String LOL_URL = "http://lol.qq.com/webplat/info/news_version3/152/4579/4581/m5583/list_1.shtml";
	private static final int SOCKET_TIMEOUT = 5 * 1000;
	private static final String LOL_URL_PRE = "http://lol.qq.com";
	private NoticeInfoCache noticeInfoCache = new NoticeInfoCache();
	private Logger logger  =  Logger.getLogger(TimeTick.class);

	// 定时推送更新通知
	public void compareNoticeUpdate() {
		List<NoticeInfo> infoList = null;
		try {
			infoList = getNoticeFromWeb();
		} catch (MalformedURLException e) {
			logger.error(e.getMessage());
			return;
		} catch (IOException e) {
			logger.error(e.getMessage());
			return;
		}
		
		if(infoList == null){
			return;
		}
		
		if(noticeInfoCache.hasNoticeInfo()){
			// 对比两者数据
			if(!noticeInfoCache.checkNoticeUpdate(infoList)){
				System.out.println("no update!!");
				return;
			}

			BaiduPushMessage msg = new BaiduPushMessage();
			msg.pushMessageForAll(msg.DEFAULT_MSG, msg.DEVICE_ANDROID,
					msg.MSG_TYPE_NOTICE_PUSH, 5 * 3600);
		}
		noticeInfoCache.setNoticeInfoList(infoList);
		ApplicationContext factory = ApplicationContextFactory.getInstance();// 初始化配置文件
		NoticeInfoMapper myMapper = (NoticeInfoMapper) factory.getBean("noticeInfoMapper");
		myMapper.deleteNoticeInfo();
		for(NoticeInfo info : infoList){
			myMapper.insertNoticeInfo(info);
		}
	}

	// 定时拉取线上通知
	public List<NoticeInfo> getNoticeFromWeb() throws MalformedURLException, IOException {
		ArrayList<NoticeInfo> noticeInfoList = new ArrayList<NoticeInfo>();
		
		// 抓取网站数据 并分析
		Document doc = null;
		doc = Jsoup.parse(new URL(LOL_URL), SOCKET_TIMEOUT);
		Element rootElement = doc.getElementById("newsListBox");
		if (rootElement == null) {
			return null;
		}
		Elements childElemnt = rootElement.getElementsByTag("p");
		Elements spanElemnt = rootElement.getElementsByTag("span");
		
		//  获取通知时间
		List<String> dateList = new ArrayList<String>();
		for (Element s : spanElemnt) {
			dateList.add(s.text());
		}

		int i = 0;
		// 获取通知内容
		for (Element t : childElemnt) {
			Elements aElemnt = t.getElementsByTag("a");
			NoticeInfo item = new NoticeInfo();
			String strItemTxt = "";
			String strUrl = "";
			for (Element a : aElemnt) {
				strItemTxt += a.text();
				strUrl = LOL_URL_PRE + a.attr("href");
			}
			strItemTxt = strItemTxt.replace("[公告]", "");
			item.setContent(strItemTxt);
			item.setUrl(strUrl);
			item.setDate_info(dateList.get(i));
			item.setModify_time(new Date());
			i++;
			noticeInfoList.add(item);
		}
		
		return noticeInfoList;
	}
}
